package com.hc.demo.utils;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Random;

/**
 * 给手机发送短信的工具类
 */
@Component
public class SendMessage {

    public void sendMessage(String phone,String message){
        //生产环境请求地址：app.cloopen.com
        String serverIp = "app.cloopen.com";
        //请求端口
        String serverPort = "8883";
        //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
        /*
        accountId: "2c94811c946f6bfb01960590ccfb4676"
  authToken: "2fd522703dac4f07a18b8524922c191e"
         */
        String accountSId ="2c94811c946f6bfb01960590ccfb4676";
       String accountToken ="2fd522703dac4f07a18b8524922c191e";
        //appId: "2c94811c946f6bfb01960590ceb6467d"
        //请使用管理控制台中已创建应用的APPID
        String appId = "2c94811c946f6bfb01960590ceb6467d";
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
        String to = phone; //绑定的测试号码作为接收方
        String templateId= "1"; //免费开发测试使用的模板ID为1
        Random random = new Random();
        StringBuilder code = new StringBuilder(message);

        Long time=2l;
        String[] datas={code.toString(),time.toString()};
        //调用容联云API发送短信
        HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas);


    }
}
